package com.lml.jdkSourceCode;

import org.junit.jupiter.api.Test;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author luomingliang
 * @date 2021/5/10 23:26
 */
public class Test01 {

    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "jack");
        Hashtable<Integer, String> map1 = new Hashtable<>();
        map1.put(1, "jack");
    }

    @Test
    public void test01() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
    }

    @Test
    public void test02() {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(1, 3);
        list.forEach(System.out::println);
    }


    /**
     * 数组转List的方式
     * https://blog.csdn.net/x541211190/article/details/79597236
     */
    @Test
    public void m1() {
        //1.通过 Arrays.asList(strArray) 方式,将数组转换List后，不能对List增删，只能查改，否则抛异常。
        String[] strArray = new String[2];
        List list = Arrays.asList(strArray);
        //对转换后的list插入一条数据
        //java.lang.UnsupportedOperationException
        list.add("1");
        System.out.println(list);
    }

    @Test
    public void m2() {
        String[] strArray = new String[2];
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(strArray));
        list.add("1");
        System.out.println(list);
    }

    @Test
    public void m3() {
        String[] strArray = new String[2];
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, strArray);
        list.add("1");
        System.out.println(list);
    }

    //Java8可通过stream流将3种基本类型数组转为List
    @Test
    public void m4() {
        List<Integer> intList = Arrays.stream(new int[]{1, 2, 3,}).boxed().collect(Collectors.toList());
        List<Long> longList = Arrays.stream(new long[]{1, 2, 3}).boxed().collect(Collectors.toList());
        List<Double> doubleList = Arrays.stream(new double[]{1, 2, 3}).boxed().collect(Collectors.toList());

        //String数组，可以使用Stream流这样转换：
        String[] arrays = {"tom", "jack", "kate"};
        List<String> stringList = Stream.of(arrays).collect(Collectors.toList());
    }
}
